import * as ts from "./node_modules/typescript/lib/typescript";
// import * as ts from "typescript";

function compile(fileNames: string[], options: ts.CompilerOptions): void {
  let program = ts.createProgram(fileNames, options);
  let emitResult = program.emit();

  let allDiagnostics = ts
    .getPreEmitDiagnostics(program)
    .concat(emitResult.diagnostics);

  allDiagnostics.forEach(diagnostic => {
    if (diagnostic.file) {
      let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start!);
      let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
      console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
    } else {
      console.log(ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"));
    }
  });

  let exitCode = emitResult.emitSkipped ? 1 : 0;
  console.log(`Process exiting with code '${exitCode}'.`);
  process.exit(exitCode);
}

const files: string[] = ['D:\\SourceCode\\tmp\\TS-Compiler-Api\\compiler-file.ts'];
const options: ts.CompilerOptions = {
  baseUrl: ".",
  outDir: "./out",
  allowJs: true,
  target: ts.ScriptTarget.ES3,
  module: ts.ModuleKind.CommonJS,
  moduleResolution: ts.ModuleResolutionKind.Classic,
  sourceMap: true,
  inlineSourceMap: false,
  inlineSources: true,
  allowSyntheticDefaultImports: true,
  forceConsistentCasingInFileNames: true,
  noImplicitReturns: true,
  noUnusedLocals: true,
  experimentalDecorators: true,
  strict: true,
  checkJs: true,
  declaration: true,
  // declarationDir: "./build/declaration",
  skipLibCheck: true
};

compile(files, options);

console.log("############# end ")